//
//  ZTodoVC.m
//  WeClubs
//
//  Created by Zed Link on 7/4/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZTodoVC.h"
#import "ZTodoNotifyVC.h"
#import "ZTodoMissionVC.h"
#import "ZTodoMeetingVC.h"

#define SELECTED_BUTTON_BG_COLOR [UIColor zColorWithHex:0x2fcafb]
#define DISSELECTED_BUTTON_BG_COLOR [UIColor zColorWithHex:0xb2c1c6]

static NSString * const TodoCellReuseID = @"ZTodoCellReuseID";

@interface ZTodoVC () <UICollectionViewDelegate, UICollectionViewDataSource>

/** 装载三个按钮的顶部视图 */
@property (weak, nonatomic) UIStackView *topView;

/** 顶部视图的选中下标线 */
@property (weak, nonatomic) UIView *selectedLine;

/** 内容滚动视图 */
@property (weak, nonatomic) UICollectionView *contentCollectView;

@end

@implementation ZTodoVC

- (void)zConfigNavigation
{
    [super zConfigNavigation];
    
    [self zAddBackItemWithGesture:YES];    
    self.navigationItem.title = @"待办事项";
    
    [self zClearNavBgColorAndSeparator:NO];
    [self.navBgView setBackgroundColor:NAVBAR_BG_COLOR];
    [self.navBgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(100);
    }];
    
    [self.navSeparatorLine setImage:[UIImage imageNamed:@"shadow_nav"]];
}

- (void)zLoadSubview
{
    [super zLoadSubview];
    
    // ========== 顶部索引栏 ==========
    UIButton *notifyBtn = [UIButton zButtonWithTitle:@"通知" fontSize:15 textColor:DISSELECTED_BUTTON_BG_COLOR];
    [notifyBtn setTitleColor:SELECTED_BUTTON_BG_COLOR forState:UIControlStateSelected];
    [notifyBtn addTarget:self action:@selector(topViewSelectedAction:) forControlEvents:UIControlEventTouchUpInside];
    notifyBtn.tag = 0;
    notifyBtn.selected = YES;
    
    UIButton *missionBtn = [UIButton zButtonWithTitle:@"任务" fontSize:15 textColor:DISSELECTED_BUTTON_BG_COLOR];
    [missionBtn setTitleColor:SELECTED_BUTTON_BG_COLOR forState:UIControlStateSelected];
    [missionBtn addTarget:self action:@selector(topViewSelectedAction:) forControlEvents:UIControlEventTouchUpInside];
    missionBtn.tag = 1;
    
    UIButton *meetingBtn = [UIButton zButtonWithTitle:@"会议" fontSize:15 textColor:DISSELECTED_BUTTON_BG_COLOR];
    [meetingBtn setTitleColor:SELECTED_BUTTON_BG_COLOR forState:UIControlStateSelected];
    [meetingBtn addTarget:self action:@selector(topViewSelectedAction:) forControlEvents:UIControlEventTouchUpInside];
    meetingBtn.tag = 2;
    
    UIStackView *topView = [[UIStackView alloc] initWithArrangedSubviews:@[notifyBtn, missionBtn, meetingBtn]];
    topView.distribution = UIStackViewDistributionFillEqually;
    topView.alignment = UIStackViewAlignmentCenter;
    
    self.topView = topView;
    [self.navBgView addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(64);
        make.height.mas_equalTo(36);
    }];
    
    // 选中高亮线
    UIView *selectedLine = [[UIView alloc] initWithFrame:CGRectZero];
    [selectedLine setBackgroundColor:SELECTED_BUTTON_BG_COLOR];
    
    self.selectedLine = selectedLine;
    [self.navBgView addSubview:selectedLine];
    [selectedLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(2);
        make.bottom.mas_equalTo(self.navBgView);
        make.centerX.mas_equalTo(notifyBtn);
    }];
    
    // ========== 内容滚动视图 ==========
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(self.view.width, self.view.height - 64 - 36);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    UICollectionView *contentCollectView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    contentCollectView.delegate = self;
    contentCollectView.dataSource = self;
    contentCollectView.pagingEnabled = YES;
    [contentCollectView setBackgroundColor:GENERAL_VIEW_BG_COLOR];
    [contentCollectView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:TodoCellReuseID];
    
    self.contentCollectView = contentCollectView;
    [self.view addSubview:contentCollectView];
    [contentCollectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.topView.mas_bottom);
        make.left.right.bottom.mas_equalTo(self.view);
    }];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    ZTodoNotifyVC *notiftyVC = [[ZTodoNotifyVC alloc] init];
    notiftyVC.clubModel = self.clubModel;
    [self addChildViewController:notiftyVC];
    
    ZTodoMissionVC *missionVC = [[ZTodoMissionVC alloc] init];
    missionVC.clubModel = self.clubModel;
    [self addChildViewController:missionVC];
    
    ZTodoMeetingVC *meetingVC = [[ZTodoMeetingVC alloc] init];
    meetingVC.clubModel = self.clubModel;
    [self addChildViewController:meetingVC];
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.topView.userInteractionEnabled = NO;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int index = (int)(scrollView.contentOffset.x + self.view.width * 0.5f) / (int)self.view.width;
//    ZLog(@"index: %zd", index);
    
    for (UIButton *btn in self.topView.arrangedSubviews) {
        btn.selected = btn.tag == index ? YES : NO;
    }
    [UIView animateWithDuration:0.25 animations:^{
        self.selectedLine.centerX = self.topView.arrangedSubviews[index].centerX;
    }];
    self.topView.userInteractionEnabled = YES;
}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.topView.arrangedSubviews.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:TodoCellReuseID forIndexPath:indexPath];
    UIViewController *controller = self.childViewControllers[indexPath.row];
    [cell.contentView addSubview:controller.view];
    controller.view.frame = cell.contentView.bounds;
    
    return cell;
}

#pragma mark - Control Action

- (void)topViewSelectedAction:(UIButton *)sender
{
    // ========== 更新选中状态的样式 ==========
    for (UIButton *btn in self.topView.arrangedSubviews) {
        btn.selected = btn == sender ? YES : NO;
    }
    [UIView animateWithDuration:0.25 animations:^{
        self.selectedLine.centerX = sender.centerX;
    }];
    
    // ========== 切换当前选中页面 ==========
    [self.contentCollectView setContentOffset:CGPointMake(self.view.width * sender.tag, 0) animated:YES];
}

@end
